package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-20
 * @Version V1.0
 **/
@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemService.findAll();
        log.debug("查询成功");
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }

    @RequestMapping("add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem) {
        log.debug("controller+...." + checkItem);
        checkItemService.add(checkItem);
        return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    @RequestMapping("findByPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.findByPage(queryPageBean);
        log.debug("查询分页成功");
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    @RequestMapping("delById")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delById(Integer id) {
        checkItemService.delById(id);
        log.debug("删除成功");
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("findById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id) {
        CheckItem checkItem = checkItemService.findById(id);
        log.debug("checkitem....." + checkItem);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @RequestMapping("edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem) {
        checkItemService.edit(checkItem);
        log.debug("修改成功");
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }
}
